package com.example.wx.dailyreport.controller;

import com.example.wx.dailyreport.pojo.User;
import com.example.wx.dailyreport.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/superadmin")
public class SuperAdminController {

    @Autowired
    private UserService userService;

    @GetMapping("/getuserinpageby_")
    public Map<String, Object> getUserInPageBy_(User user, Long role_flag, Long authority_flag, @RequestParam(defaultValue = "1") int page, int limit) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("status", 200);
        map.put("data", userService.selectUserInPage(user, role_flag, authority_flag, page, limit));
        map.put("count", userService.selectUserCount(user, role_flag, authority_flag));
        return map;
    }

    @GetMapping("/getauthoritiesbyuserid")
    public Map<String, Object> getAuthoritiesByUserId(Integer id) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("data", userService.getAuthoritiesByUserId(id));
        map.put("status", 200);
        return map;
    }

    @PostMapping("/updateuser")
    public Map<String, Object> updateUser(Integer id, Integer[] roleids, Integer[] addAuthorities, Integer[] delAuthorities) {
        Map<String, Object> map = new LinkedHashMap<>();
        if (userService.updateUserRoleAuthority(id, roleids, addAuthorities, delAuthorities))
            map.put("status", 200);
        else
            map.put("status", 400);
        return map;
    }

    @PostMapping("/updateusersurvival")
    public Map<String, Object> updateUserSurvival(User user) {
        Map<String, Object> map = new LinkedHashMap<>();
        if (userService.updateById(user))
            map.put("status", 200);
        else
            map.put("status", 400);
        return map;
    }

}
